package com.cloudsea.yunhaistores.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cloudsea.yunhaistores.entity.PaymentOrders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.cloudsea.yunhaistores.vo.DashboardSummaryVO;
import org.apache.ibatis.annotations.Param;
import java.time.LocalDateTime;

@Mapper
public interface PaymentOrdersMapper extends BaseMapper<PaymentOrders> {

    /**
     * 【新增】查询指定商户在某个时间段内的看板数据摘要
     * @param merchantId 商户ID
     * @param startTime  统计开始时间
     * @param endTime    统计结束时间
     * @return DashboardSummaryVO
     */
    @Select("SELECT " +
            "   COALESCE(SUM(CASE WHEN pay_status = 1 THEN total_amount ELSE 0 END), 0) AS totalTurnover, " +
            "   COALESCE(COUNT(CASE WHEN pay_status = 1 THEN 1 END), 0) AS totalOrders, " +
            "   COALESCE(COUNT(CASE WHEN fulfillment_status = 1 THEN 1 END), 0) AS pendingOrders " +
            "FROM payment_orders " +
            "WHERE merchant_id = #{merchantId} AND created_at BETWEEN #{startTime} AND #{endTime}")
    DashboardSummaryVO getDashboardSummary(@Param("merchantId") Long merchantId,
                                           @Param("startTime") LocalDateTime startTime,
                                           @Param("endTime") LocalDateTime endTime);
}
